/**********************************************************
 * LockSystemAccessAction.java
 * Created by ankur
 * Last modified Date: Aug 26, 2008 11:26:41 AM by ankur
 * Version : 0.1
 * Copyright (c) 2008 - 2008 cyberThink Infotech Pvt. Ltd., All rights reserved.
 **********************************************************/

package com.printeverywhere.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletContext;

import org.apache.log4j.Logger;
import org.apache.struts2.util.ServletContextAware;

import com.printeverywhere.util.AdminConstants;
import com.printeverywhere.util.MessageConstants;

/**
 * The Class LockSystemAccessAction.
 */
public class LockSystemAccessAction extends AdminSystemTools implements ServletContextAware  {

	/** Logger for this class. */
	private static final Logger logger = Logger.getLogger(LockSystemAccessAction.class);

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 1L;

	/** The application. */
	private ServletContext application;

	/** The system_acesss. */
	private List<String> system_acesss;

	/** The access. */
	private String access;

	/**
	 * Gets the access.
	 *
	 * @return the access
	 */
	public String getAccess() {
		return access;
	}

	/**
	 * Sets the access.
	 *
	 * @param access the new access
	 */
	public void setAccess(String access) {
		this.access = access;
	}

	/**
	 * Gets the system_acesss.
	 *
	 * @return the system_acesss
	 */
	public List<String> getSystem_acesss() {
		return system_acesss;
	}

	/**
	 * Sets the system_acesss.
	 *
	 * @param system_acesss the new system_acesss
	 */
	public void setSystem_acesss(List<String> system_acesss) {
		this.system_acesss = system_acesss;
	}

	/* (non-Javadoc)
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	public String execute() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("execute() - start"); //$NON-NLS-1$
		}
		if((Boolean) application.getAttribute(AdminConstants.SYSTEM_ACCESS)){
			access = getText(MessageConstants.SYSTEM_UN_LOCK);
		}else{
			access = getText(MessageConstants.SYSTEM_LOCK);
		}
		
		system_acesss = new ArrayList<String>();
		system_acesss.add(getText(MessageConstants.SYSTEM_LOCK));
		system_acesss.add(getText(MessageConstants.SYSTEM_UN_LOCK));

		if (logger.isDebugEnabled()) {
			logger.debug("execute() - end"); //$NON-NLS-1$
		}
		return SUCCESS;
	}
	

	/**
	 * Change access.
	 *
	 * @return the string
	 */
	public String changeAccess() {
		if (logger.isDebugEnabled()) {
			logger.debug("changeAccess() - start"); //$NON-NLS-1$
		}
		if(access==null){
			addActionMessage("Please select an option");
		}
		else if (access!=null&&access.equals(getText(MessageConstants.SYSTEM_LOCK))) {
			application.setAttribute(AdminConstants.SYSTEM_ACCESS,AdminConstants.FALSE);
			addActionMessage(getText(MessageConstants.SYSTEM_LOCK_INFO));
		}
		else if (access!=null&&access.equals(getText(MessageConstants.SYSTEM_UN_LOCK))) {
			application.setAttribute(AdminConstants.SYSTEM_ACCESS,AdminConstants.TRUE);
			addActionMessage(getText(MessageConstants.SYSTEM_UNLOCK_INFO));
		}

		if (logger.isDebugEnabled()) {
			logger.debug("changeAccess() - end"); //$NON-NLS-1$
		}
		return SUCCESS;
	}

	/* (non-Javadoc)
	 * @see org.apache.struts2.util.ServletContextAware#setServletContext(javax.servlet.ServletContext)
	 */
	public void setServletContext(ServletContext application) {
		this.application = application;
	}

	/**
	 * Gets the application.
	 *
	 * @return the application
	 */
	public ServletContext getApplication() {
		return application;
	}

	/**
	 * Sets the application.
	 *
	 * @param application the new application
	 */
	public void setApplication(ServletContext application) {
		this.application = application;
	}
}
